#ifndef STORAGE_H
#define STORAGE_H

#include <xdevice/xdevice.h>
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 本地存储后端 */
typedef struct {
    char base_path[MAX_PATH_LEN];
    bool initialized;
} local_backend_t;

/* NFS存储后端 */
typedef struct {
    char mount_point[MAX_PATH_LEN];
    bool initialized;
} nfs_backend_t;

/* NVMe Fabric存储后端 */
typedef struct {
    char target_address[256];
    char target_ip[64];
    int target_port;
    bool initialized;
} nvme_fabric_backend_t;

/* Mock存储后端 */
typedef struct {
    char base_path[MAX_PATH_LEN];
    bool initialized;
} mock_backend_t;

/* 统一存储接口实际结构体定义 */
struct storage_interface {
    storage_backend_type_t backend_type;
    char base_path[MAX_PATH_LEN];
    
    union {
        local_backend_t local;
        nfs_backend_t nfs;
        nvme_fabric_backend_t nvme_fabric;
        mock_backend_t mock;
    } backend;
};

/* 存储接口API */
int storage_interface_init(storage_interface_t *interface, 
                          storage_backend_type_t type, 
                          const char *path);

int storage_interface_create_device(storage_interface_t *interface,
                                   const char *device_name, 
                                   uint64_t size);

int storage_interface_write(storage_interface_t *interface,
                           const char *device_name,
                           uint64_t offset,
                           const void *buffer,
                           size_t length);

int storage_interface_read(storage_interface_t *interface,
                          const char *device_name,
                          uint64_t offset,
                          void *buffer,
                          size_t length);

int storage_interface_sync(storage_interface_t *interface,
                          const char *device_name);

void storage_interface_cleanup(storage_interface_t *interface);

/* 各后端具体实现 */

/* 本地存储后端 */
int local_backend_init(local_backend_t *backend, const char *base_path);
int local_backend_create_device(local_backend_t *backend, const char *device_name, uint64_t size);
int local_backend_write(local_backend_t *backend, const char *device_name,
                       uint64_t offset, const void *buffer, size_t length);
int local_backend_read(local_backend_t *backend, const char *device_name,
                      uint64_t offset, void *buffer, size_t length);
int local_backend_sync(local_backend_t *backend, const char *device_name);
void local_backend_cleanup(local_backend_t *backend);

/* NFS存储后端 */
int nfs_backend_init(nfs_backend_t *backend, const char *mount_point);
int nfs_backend_create_device(nfs_backend_t *backend, const char *device_name, uint64_t size);
int nfs_backend_write(nfs_backend_t *backend, const char *device_name,
                     uint64_t offset, const void *buffer, size_t length);
int nfs_backend_read(nfs_backend_t *backend, const char *device_name,
                    uint64_t offset, void *buffer, size_t length);
int nfs_backend_sync(nfs_backend_t *backend, const char *device_name);
void nfs_backend_cleanup(nfs_backend_t *backend);

/* NVMe Fabric存储后端 */
int nvme_fabric_backend_init(nvme_fabric_backend_t *backend, const char *target_address);
int nvme_fabric_backend_create_device(nvme_fabric_backend_t *backend, const char *device_name, uint64_t size);
int nvme_fabric_backend_write(nvme_fabric_backend_t *backend, const char *device_name,
                        uint64_t offset, const void *buffer, size_t length);
int nvme_fabric_backend_read(nvme_fabric_backend_t *backend, const char *device_name,
                       uint64_t offset, void *buffer, size_t length);
int nvme_fabric_backend_sync(nvme_fabric_backend_t *backend, const char *device_name);
void nvme_fabric_backend_cleanup(nvme_fabric_backend_t *backend);

/* Mock存储后端 */
int mock_backend_init(mock_backend_t *backend, const char *base_path);
int mock_backend_create_device(mock_backend_t *backend, const char *device_name, uint64_t size);
int mock_backend_write(mock_backend_t *backend, const char *device_name,
                      uint64_t offset, const void *buffer, size_t length);
int mock_backend_read(mock_backend_t *backend, const char *device_name,
                     uint64_t offset, void *buffer, size_t length);
int mock_backend_sync(mock_backend_t *backend, const char *device_name);
void mock_backend_cleanup(mock_backend_t *backend);

#ifdef __cplusplus
}
#endif

#endif // STORAGE_H
